<?php

Yii::import('application.models.catalog.Product');

class Cart extends CApplicationComponent
{
    protected $items=array(), $message='', $lastAdded, $userId;

    public function init()
    {
        $this->userId=Yii::app()->user->id;
    }

    public function add($id,$count)
    {
        $result=false;

        if(is_array($id)){
            foreach($id as $key){
                if($this->add($key)) $result=true;
            }
        }else{
            if($product=Product::model()->findByPk($id)){
                $newProd=array(
                    'id'=>$id,
                    'count'=>$count,
                    'name'=>$product->name,
                    'remove'=>$product->remove,
                );
                foreach($product->attributes as $name=>$value){
                    $newProd[$name]=$value;
                }

                try{
                    $cart=Yii::app()->session['cart'];
                    $cart['lastAdded']=$newProd;
                    $this->lastAdded=$newProd;

                    if(isset($cart['products'][$id])) $newProd['count']+=$cart['products'][$id]['count'];
                    $cart['products'][$id]=$newProd;

                    $this->items=$cart['products'];
                    Yii::app()->session['cart']=$cart;

                    $result=true;
                }catch(Exception $e){
                    $this->message.=$e->getCode();
                }
            }else{
                $this->message.=Yii::t('catalog/products','errorNotFound',array('id'=>$id));
            }
        }

        return $result;
    }

    public function remove($id,$count=0)
    {
        $result=false;
        if(is_array($id)){
            foreach($id as $key){
                if($this->remove($key)) $result=true;
            }
        }else{
            if(!$product=$this->getProduct($id)){
                $this->message.=Yii::t('catalog/products','errorNotFound',array('id'=>$id)).' /n';
                $result=false;
            }else{
                try{
                    $this->getProducts();
                    $lastAdded=$this->getLastAdded();

                    if($count) $this->items[$id]['count']-=$count;
                    else unset($this->items[$id]);

                    Yii::app()->session['cart']=array(
                        'products'=>$this->items,
                    );

                    unset($lastAdded);
                    $result=$product;
                }catch(Exception $e){
                    $this->message.=$e->getCode().' /n';
                }
            }
        }

        return $result;
    }

    public function getMessage()
    {
        return $this->message;
    }

    public function getLastAdded()
    {
        if($this->lastAdded) return (object)$this->lastAdded;
        else{
            $cart=Yii::app()->session['cart'];
            if(isset($cart['products'])){
                $this->items=$cart['products'];
                $this->lastAdded=end($cart['products']);
                unset($cart);
                return (object)$this->lastAdded;
            }
        }
        return false;
    }

    public function getProduct($id)
    {
        if(isset($this->items[$id])) return (object)$this->items[$id];
        else{
            $cart=Yii::app()->session['cart'];
            if(isset($cart['products'])){
                $this->items=$cart['products'];
                unset($cart);
            }
            if(isset($this->items[$id])) return (object)$this->items[$id];
        }
        return false;
    }

    public function getProducts()
    {
        if($this->items) return $this->convertToObjects($this->items);
        else{
            $cart=Yii::app()->session['cart'];
            if(isset($cart['products'])){
                $this->items=$cart['products'];
                unset($cart);
                return $this->convertToObjects($this->items);
            }
        }
        return false;
    }

    public function getProductsArr()
    {
        if($this->items) return $this->items;
        else{
            $cart=Yii::app()->session['cart'];
            if(isset($cart['products'])){
                $this->lastAdded=end($cart['products']);
                $this->items=$cart['products'];
                unset($cart);
                return $this->items;
            }
        }
        return false;
    }

    public function getHesProducts()
    {
        $products=$this->getProducts();
        if($products && sizeof($products)>0){
            unset($products);
            return true;
        }else
            return false;
    }

    public function getCount()
    {
        $count=0;
        if($products=$this->getProducts()){
            $count=sizeof($products);
            unset($products);
        }
        return $count;
    }

    protected function convertToObjects($products)
    {
        if(!is_array($products)){
            $this->message.=Yii::t('error','isNotArray',array('function'=>'Yii::app()->cart->convertToObjects($array)'));
            return false;
        }

        $result=array();
        foreach($products as $id=>$prod){
            $result[$id]=(object)$prod;
        }
        return $result;
    }
}